package org.csu.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore.common.CommonResponse;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.mapper.*;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private CartMapper cartMapper;

    @Override
    public CommonResponse<Category> getCategoryById(String categoryId) {
        Category category=categoryMapper.selectById(categoryId);
        if(category == null){
            return CommonResponse.createForError("数据库中没有以"+categoryId+"为id的Category类");
        }

        return CommonResponse.createForSuccess(category);
    }

    @Override
    public CommonResponse<List<Category>> getCategoryList() {
        List<Category> categoryList=categoryMapper.selectList(null);
        if(categoryList.isEmpty()){
            return CommonResponse.createForError("查询CategoryList为空");
        }

        return CommonResponse.createForSuccess(categoryList);
    }

    @Override
    public CommonResponse<Product> getProductById(String productId) {
        Product product=productMapper.selectById(productId);
        if(product == null){
            return CommonResponse.createForError("数据库中没有以"+productId+" 为id的Product类");
        }

        return CommonResponse.createForSuccess(product);
    }

    @Override
    public CommonResponse<List<Product>> getProductList() {
        List<Product> productList=productMapper.selectList(null);
        if(productList.isEmpty()){
            return CommonResponse.createForError("查询ProductList失败");
        }

        return CommonResponse.createForSuccess(productList);
    }

    @Override
    public CommonResponse<List<Product>> getProductListByCategoryId(String categoryId) {
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category", categoryId);
        List<Product> productList=productMapper.selectList(queryWrapper);
        if(productList.isEmpty()){
            return CommonResponse.createForError("数据库中没有以"+categoryId+"为category的ProductList");
        }

        return CommonResponse.createForSuccess(productList);
    }

    @Override
    public CommonResponse<Item> getItemById(String itemId) {
        Item item=itemMapper.selectById(itemId);
        if(item == null){
            return CommonResponse.createForError("数据库中没有以"+itemId+"为id的Item类");
        }
        return CommonResponse.createForSuccess(item);
    }

    @Override
    public CommonResponse<List<Item>> getItemList() {
        List<Item> itemList=itemMapper.selectList(null);
        if(itemList.isEmpty()){
            return CommonResponse.createForError("查询ItemList失败");
        }

        return CommonResponse.createForSuccess(itemList);
    }

    @Override
    public CommonResponse<List<Item>> getItemListByProductId(String productId) {
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("productid", productId);
        List<Item> itemList=itemMapper.selectList(queryWrapper);
        if(itemList.isEmpty()){
            return CommonResponse.createForError("数据库中没有以"+productId+"为productid的ItemList");
        }
        return CommonResponse.createForSuccess(itemList);
    }

    @Override
    public CommonResponse<List<Category>> searchCategory(String keyword) {
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name", keyword);
        List<Category> categoryList=categoryMapper.selectList(queryWrapper);
        if(categoryList.isEmpty()){
            return CommonResponse.createForError("未搜索到关键字"+keyword+"对应Category类");
        }
        return CommonResponse.createForSuccess(categoryList);
    }

    @Override
    public CommonResponse<List<Product>> searchProduct(String keyword) {
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name", keyword);
        List<Product> productList=productMapper.selectList(queryWrapper);
        if(productList.isEmpty()){
            return CommonResponse.createForError("未搜索到关键字"+keyword+"对应Product类");
        }
        return CommonResponse.createForSuccess(productList);
    }

    @Override
    public CommonResponse<List<Item>> searchItem(String keyword) {
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("itemid", keyword);
        List<Item> itemList=itemMapper.selectList(queryWrapper);
        if(itemList.isEmpty()){
            return CommonResponse.createForError("未搜索到关键字"+keyword+"对应Item类");
        }
        return CommonResponse.createForSuccess(itemList);
    }

    @Override
    public boolean checkItemQuantity(List<Cart> cartList) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        for(Cart cart: cartList){
            queryWrapper.eq("itemid", cart.getItemId());
            Item item = itemMapper.selectOne(queryWrapper);
            if(item.getQuantity() < cart.getQuantity()){
                return false;
            }
        }
        return true;
    }
}

